<?php

class Silk_Tms_Block_Adminhtml_Shipping_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId("shippingGrid");
        $this->setDefaultSort("created_at");
        $this->setDefaultDir("DESC");
        $this->setSaveParametersInSession(true);
    }

    public function getItemCollection()
    {
        $this->_prepareCollection();
        return $this->setCollection();
    }

    protected function _prepareCollection()
    {
        /** @var Silk_Tms_Model_Mysql4_Shipping_Collection $collection */
        $collection = Mage::getModel('tms/shipping')->getCollection();
        if ($this->getWebSiteId()) {
            $collection
                ->addFieldtoFilter('main_table.website_id', array('eq' => $this->getWebSiteId()));
        }

        /** @var Mage_Admin_Model_Session $adminSession */
        $adminSession = Mage::getModel('admin/session');
        if ($adminSession->isAllowed('sales/shipping/actions/unlock') && $this->getWebsiteId()) {
            $collection->addFieldToFilter('lock', 1);
        }
        //add by wanglin start  异常区订单加入运单只显示分拣完成
        $isorder = $this->getRequest()->getParam('orderId');
        $addflag = $this->getRequest()->getParam('addflag');
        if($isorder && !empty($isorder) && $addflag){
        	$collection->addFieldtoFilter('main_table.status', array('eq' => 'sorted'));
        }
        //add by wanglin end

        $select = $collection->getSelect();
        $select->joinLeft(array('au' => 'admin_user'), "main_table.admin_id=au.user_id and main_table.website_id = au.website_id", array('username' => 'au.username'));
        $collection->addFilterToMap('username', 'au.username');

        $select->joinLeft(array('ad' => 'admin_user'), "main_table.user_id=ad.user_id and main_table.website_id = ad.website_id", array('adusername' => 'ad.username'));
        $collection->addFilterToMap('adusername', 'ad.username');

        $select->joinLeft(array('region' => 'tms_region'), 'region.id=main_table.region_id',
            array('regionname' => 'region.name'));
        $collection->addFilterToMap('regionname', 'region.name');

        $select->joinLeft(array('car' => 'tms_car'), 'car.id=main_table.car_id',
            array('carnumber' => 'car.number'));
        $collection->addFilterToMap('carnumber', 'car.number');
        
        
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function getWebSiteId()
    {
        $session = Mage::getSingleton('admin/session');
        return $session->getUser()->getWebsiteId();
    }

    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/view', array('id' => $row->getId()));
    }

    public function getGridUrl()
    {
        return $this->getUrl('admin_tms/adminhtml_shipping/index', array('_current' => true));
    }

    protected function _prepareColumns()
    {
        $this->addColumn("increment_id", array(
            "header" => Mage::helper("tms")->__("ID"),
            "align" => "right",
            "width" => "80px",
            "type" => "text",
            "index" => "increment_id",
        ));

        $this->addColumn("status", array(
            "header" => Mage::helper("tms")->__("状态"),
            "index" => "status",
            'type' => 'options',
            'options' => Mage::helper("tms/status")->getShippingStatusOptions(),
        ));

        $this->addColumn("total_order", array(
            "header" => Mage::helper("tms")->__("订单总数"),
            "index" => "total_order",
        ));

        $this->addColumn("order_delivered", array(
            "header" => Mage::helper("tms")->__("配送完成订单数"),
            "index" => "order_delivered",
            "renderer" => "Silk_Zgb_Block_Renderer_Delivered"
        ));

        // $this->addColumn("items_total", array(
        //     "header" => Mage::helper("tms")->__("商品数"),
        //     "index" => "total_item_count",
        //     "renderer" => "Silk_Zgb_Block_Renderer_ItemsTotal"
        // ));

        $this->addColumn("items_total_all", array(
            "header" => Mage::helper("tms")->__("商品总数"),
            "index" => "total_qty_ordered",
            "renderer" => "Silk_Zgb_Block_Renderer_ItemsTotalAll"
        ));

        $this->addColumn("grand_total", array(
            "header" => Mage::helper("tms")->__("运单总价"),
            "index" => "grand_total",
        ));

        $this->addColumn("total_paid", array(
            "header" => Mage::helper("tms")->__("实际支付"),
            "index" => "total_paid",
        ));

        $this->addColumn("comment", array(
            "header" => Mage::helper("tms")->__("运单备注"),
            "index" => "comment",
        ));
        $this->addColumn("regionname", array(
            "header" => Mage::helper("tms")->__("分拣区域"),
            "index" => "regionname",
        ));
        $this->addColumn("carnumber", array(
            "header" => Mage::helper("tms")->__("车载编号"),
            "index" => "carnumber",
        ));
        $this->addColumn("username", array(
            "header" => Mage::helper("tms")->__("操作管理员"),
            "index" => "username",
        ));

        $this->addColumn("adusername", array(
            "header" => Mage::helper("tms")->__("配送员"),
            "index" => "adusername",
        ));

        $lock = array('0' => '未锁定', '1' => '已锁定');
        $this->addColumn("lock", array(
            "header" => Mage::helper("tms")->__("是否锁定"),
            "index" => "lock",
            'type' => 'options',
            'options' => $lock,
        ));

        $this->addColumn("created_at", array(
            "header" => Mage::helper("tms")->__("创建时间"),
            "index" => "created_at",
            'width' => 1,
            'type' => 'datetime',
            'align' => 'center',
            'default' => $this->__('N/A'),
            'html_decorators' => array('nobr')
        ));

        $this->addColumn("updated_at", array(
            "header" => Mage::helper("tms")->__("更新时间"),
            "index" => "updated_at",
            'width' => 1,
            'type' => 'datetime',
            'align' => 'center',
            'default' => $this->__('N/A'),
            'html_decorators' => array('nobr')
        ));

        if ($orderIds = Mage::getSingleton("core/session")->getData("add_orderids")) {
            $this->addColumn('action',
                array(
                    'header' => Mage::helper('tms')->__('操作'),
                    'width' => '100px',
                    'type' => 'action',
                    'renderer' => 'Silk_Zgb_Block_Renderer_Url',
                    'filter' => false,
                    'sortable' => false,
                    'index' => 'stores',
                ));
        }

        return parent::_prepareColumns();
    }

    public function _prepareMassaction()
    {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('shipping_ids');
        $this->getMassactionBlock()->setUseSelectAll(false);

        $this->getMassactionBlock()->addItem('print_shipping_label', array(
             'label' => Mage::helper('sales')->__('打印运单'),
             'url' => $this->getUrl('*/adminhtml_shipping/print'),
         ));
        if(Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/lock')){
            $this->getMassactionBlock()->addItem('lock', array(
             'label' => Mage::helper('sales')->__('锁定运单'),
             'url' => $this->getUrl('*/adminhtml_shipping/lock'),
            ));
        }
        if(Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/unlock')){
            $this->getMassactionBlock()->addItem('unlock', array(
                 'label' => Mage::helper('sales')->__('解锁运单'),
                 'url' => $this->getUrl('*/adminhtml_shipping/unlock'),
             ));
        }

        return parent::_prepareMassaction();
    }
}